#include <header.h>

int main(){

    char *port = "8080";
    char *ip = "192.168.106.130";

    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);


    struct sockaddr_in sock;
    sock.sin_family = AF_INET;
    sock.sin_port = htons(atoi(port));
    sock.sin_addr.s_addr = inet_addr(ip);
    connect(socket_fd, (struct sockaddr *)&sock, sizeof(sock));

    // 连接建立
    int flag = 1; // 1代表链接正常, 没有被踢出 , 0代表被踢出
    while(1){
        fd_set set;
        FD_ZERO(&set);
        FD_SET(STDIN_FILENO, &set);
        if(flag == 1){
            FD_SET(socket_fd, &set);
        }

        select(socket_fd+1, &set, NULL, NULL, NULL);

        if(FD_ISSET(STDIN_FILENO, &set)){
            if(flag == 0){
                socket_fd = socket(AF_INET, SOCK_STREAM, 0);
                connect(socket_fd, (struct sockaddr *)&sock, sizeof(sock));

                flag = 1;
            }
            char buf[100] = {0};
            read(STDIN_FILENO, buf, sizeof(buf));
            send(socket_fd, buf, sizeof(buf), 0);

        }

        if(FD_ISSET(socket_fd, &set)){
            char buf[100] = {0};
            ssize_t ret = recv(socket_fd, buf, sizeof(buf), 0);
            if(ret == 0){

               // printf("对方断开链接 \n");
               // break;
                flag = 0;
                close(socket_fd);
                continue;
            }
            printf("buf: %s \n", buf);
        }
    }

    close(socket_fd);
    return 0;
}

